package MicroRpc.framework.protocol.dubbo.jdk;

import MicroRpc.framework.commons.StringUtils;
import MicroRpc.framework.excption.DubboException;
import MicroRpc.framework.beans.Url;
import MicroRpc.framework.protocol.dubbo.protobuf.InvokerProto;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "m.NettyServer")
public class NettyServer {
    public static void main(String[] args) {
        serverStart(new Url("localhost",8080));
    }
    public static void serverStart(Url url){
        if (!StringUtils.hasText(url.getHost()) || null==url.getPort())
            throw new DubboException("检查Url是否书写正确");

        serverStart0(url);
    }

    private static void serverStart0(Url url){
        EventLoopGroup bossGroup=new NioEventLoopGroup(1);
        // 核数*总等待时间 / cpu等待时间 约等于 8 *100 /10 =80  ==>Runtime.getRuntime().availableProcessors() * 10
        EventLoopGroup wokerGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap=new ServerBootstrap();
        serverBootstrap
                .group(bossGroup,wokerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel channel) throws Exception {
                        ChannelPipeline pipeline = channel.pipeline();
                        pipeline.addLast(new ProtobufDecoder(InvokerProto.InvokerMsg.getDefaultInstance()));
                        pipeline.addLast(new ByteArrayEncoder());// 返回数据应该编码成result
                        pipeline.addLast(new ServerHandler());
                    }
                });
        ChannelFuture channelFuture = serverBootstrap.bind(url.getHost(), url.getPort());
        log.info("dubbo-server 开始提供服务.");
        try {
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            wokerGroup.shutdownGracefully();
        }
    }
}
